<template>
  <div  class="flex" style="padding-left: 10px;">
    <form  class="form-horizontal">
        <p class="bg-info text-center" style="padding: 8px;" >批量开户</p>
       <div class="row" style="margin-top:10px;">
	   <div class="col-sm-3 form-input-group">
           <label for="metertype"  class="col-sm-1 control-label">气表类型:</label>
           <v-select :value.sync="metermessage.metertype"
           :options='meterType' placeholder='预付类型'
           v-model="metermessage.metertype"
           close-on-select></v-select>
    </div>
        <div class="col-sm-3 form-input-group" style= "margin-left:70px;">
          <label for="prepaytype"  class="col-sm-2 control-label">预付类型:</label>
          <input class="form-control"  type="text"  v-model="metermessage.prepaytype"  disabled="true">
        </div>
    </div>
    </form>
    <div style="text-align:right;height:auto;margin-top:80px;margin-right:50px;">
      <button class="btn btn-success " @click="confirm()" style="width:80px;margin-left:20px;" :disabled='!$v.valid' >确&nbsp;&nbsp;&nbsp;定</button>
       <button class="btn btn-default" @click="cancel()" style="width:80px;margin-left:20px;">取&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;消</button>
    </div>
<!-- </div> -->
  </div>
</template>

<script>
import AppData from '../../../stores/AppData'
import * as Util from '../../../components/Util'
export default {
  title: '开户',
  data () {
    return {
      print: false,
      metermessage: {
        prepaytype: '后付费',
        metertype: []
      },
      param: {
        type: '开户',
        f_filiale: Util.f.f_fengongsi,
        f_outlets: Util.f.f_parentname
      }
    }
  },
  props: ['data'],
  methods: {
    confirm () {
      this.transfer()
      this.laterPayUser()
    },
    prePayUser () {
      this.logicmodel.f_user_id = this.row.f_user_id
      this.metermessage.rechargeamount = this.changeTwoDecimal(this.metermessage.rechargeamount)
      this.$WebMeterService.prepayUserOpenAccount(this.metermessage, this.logicmodel, this.row, this.param)
      .then(
        (res) => {
          this.$dispatch('success', '开户', this.row, res)
        }
      ).catch(
        (error) => {
          this.$dispatch('error', '开户', this.data, error)
        }
      )
    },
    laterPayUser () {
      this.metermessage.prepaytype = 0
      this.metermessage.rechargequantity = this.changeTwoDecimal(0)
      this.metermessage.rechargeamount = this.changeTwoDecimal(0)
      this.$WebMeterService.bulkOpenAccount(this.metermessage, this.data, this.param)
      // .then(
      //   (res) => {
      //     this.$dispatch('success', '开户', this.data, res)
      //   }
      // ).catch(
      //   (error) => {
      //     this.$dispatch('error', '开户', this.data, error)
      //   }
      // )
    },
    close () {
      this.showprint = false
      this.print = false
      this.cancel()
    },
    cancel () {
      this.$info('取消操作')
      this.$dispatch('clean', '开户')
    },
    transfer () {
      if (this.metermessage.metertype[0] === '气量表') {
        this.metermessage.metertype = 0
      } else if (this.metermessage.metertype[0] === '金额表') {
        this.metermessage.metertype = 1
      }
    },
    changeTwoDecimal (number) {
      var firstnumber = Math.round(number * 100) / 100
      var secondnumber = firstnumber.toString()
      var pos_decimal = secondnumber.indexOf('.')
      if (pos_decimal < 0) {
        pos_decimal = secondnumber.length
        secondnumber += '.'
      }
      for (var i = secondnumber.length; i <= pos_decimal + 2; i++) {
        secondnumber += '0'
      }
      return secondnumber
    }
  },
  computed: {
    meterType () {
      return AppData.getParam('物联网表类型')
    },
    prepayType () {
      return AppData.getParam('预付类型')
    }
  }
}
</script>
